home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
C⁄C++
/
E-Z Progress Bar 1.0a
/
main.cp
< prev
next >
Wrap
Text File
|
1995-03-15
|
4KB
|
122 lines
#include "ProgressClass.h"
void InitToolbox(void);
void GetStringX(Str255 s1, Str255 s2, short i);
void InitToolbox()
{
InitGraf((Ptr) &qd.thePort);
InitFonts();
InitWindows();
InitMenus();
FlushEvents(everyEvent,0);
TEInit();
InitDialogs(0L);
InitCursor();
}
main()
{
InitToolbox();
ProgressClass myBar(progressDefaults-fineChisel); // options
// An instance of ProgressClass is created. All default options except the
// fineChisel effect are turned on.
Str255 theString;
short i;
Boolean cancel=0, deactivate;
WindowPtr updateWindow;
// The following call will set a minimum and a maximum bar value of 0 and 20,
// respectively, so that when a number between that range is passed via "SetBar"
// the proper bar percentage is automatically displayed. SetTexts will set the
// window title to "Progress Window" and the parameter text above the bar to
// "Progress Bar". Later, SetBar calls with a string in the second argument
// will change this parameter text.
// Finally, ShowBarWindow sets the port to the progress bar window and displays
// it. The class will always set the port back to the original port when it
// finishes with a task (or at least it should - let me know if you have problems).
myBar.SetMinMax(0, 20);
myBar.SetTexts("\pProgress Window", "\pProgress Bar");
myBar.ShowBarWindow();
for (i=0; i<=20 && !cancel; i++) {
if (!myBar.ReceivedEvent(&cancel, &deactivate, &updateWindow)) {
// If no event concerning you has been received, continue with
// your task here. Here, a number is converted to a string and the new
// number is posted along with the new update to the progress bar in
// the SetBar call (sets the bar to a new position)
GetStringX("\pFirst Round ", theString, i);
myBar.SetBar(i, theString);
Delay(10, nil);
}
else {
// An event has been received. Examine the variables passed in the
// function parameters to find out what event has transpired.
// If the user has cancelled, the first paramater will be true, while
// the others will be false (or nil). When your window is deactivated,
// deactivate will return true and the window's pointer will be in
// the third paramater. If you receive an update event,
// the window to be updated will be returned in the third parameter.
if (cancel)
; // User cancelled, cease and desist
else if (deactivate)
; // Your window is in the background;
// do whatever is necessary to be in backgound
else if (updateWindow)
; // Update this window
}
}
// The next call will create a custom control, using the CNTL resource 128
// If it is not found, the default cancel button is used
// The bar height is also set to 15 pixels
myBar.SetOptions(progressDefaults); // This will turn on all defaults
myBar.SetControlID(128);
myBar.SetBarHeight(15);
cancel=0;
for (i=0; i<=20 && !cancel; i++) {
if (!myBar.ReceivedEvent(&cancel, &deactivate, &updateWindow)) {
// In this example, we are setting the progress bar via a float variable,
// whereas before it was a short. You can use a float to represent a
// percentage of the task being done, which means this will only
// represent numbers in the range of 0.0 to 1.0. All other values will be
// considered zero if less than zero, or one if greater than one.
myBar.SetBar((float) ((float)i/20.0), "\pYour action name here…");
Delay(10, nil);
}
else
; // examine parameters here as above
}
return 0;
}
void GetStringX(Str255 s1, Str255 s2, short i)
{
Str255 sNumber;
NumToString(i, sNumber);
s2[0]=s1[0]+sNumber[0];
for (i=1; i<=s1[0]; i++)
s2[i]=s1[i];
for (i=1; i<=sNumber[0]; i++)
s2[s1[0]+i]=sNumber[i];
}